package vn.edu.saigontech.ncms.web.study;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import vn.edu.saigontech.ncms.model.info.Offering;
import vn.edu.saigontech.ncms.model.info.OfferingMember;
import vn.edu.saigontech.ncms.model.info.AbstractReceipt;
import vn.edu.saigontech.ncms.model.result.FinalTest;
import vn.edu.saigontech.ncms.model.result.SecondTest;
import vn.edu.saigontech.ncms.service.OfferingMemberService;
import vn.edu.saigontech.ncms.service.OfferingService;
import vn.edu.saigontech.ncms.service.SecondTestService;
import vn.edu.saigontech.ncms.service.UserService;
import vn.edu.saigontech.ncms.util.Checker;
import vn.edu.saigontech.ncms.util.DateUtil;
import vn.edu.saigontech.ncms.web.BaseAction;

@SuppressWarnings("serial")
public class SecondFinalTestAction extends BaseAction {
	private Long id;
	private Long memberId;
	private Long receiptId;
	private Long offeringId;

	private String finalSpokenDate;
	private String finalWrittenDate;

	private Double writtenScore;
	private Double speakingScore;
	private Double listeningScore;
	private Double grammarScore;
	private Double readingScore;
	private String comment;

	public String list() {
		Offering offering = OfferingService.INSTANCE.findById(this.offeringId);
		List<SecondTest> tests = offering.getSecondFinalTests();
		if (tests.isEmpty())
			return ADD;
		REQUEST().setAttribute("offering", offering);
		return SUCCESS;
	}

	public String showAdd() {
		Offering offering = OfferingService.INSTANCE.findById(this.offeringId);
		Set<AbstractReceipt> receipts = offering.getOfferingReceipts();
		List<AbstractReceipt> ret = new ArrayList<AbstractReceipt>();
		for (AbstractReceipt receipt : receipts) {
			FinalTest test = receipt.getMember().getFinalTest();
			SecondTest secondTest = receipt.getMember().getSecondFinalTest();
			if (test.isAllGradeInput() && Checker.isNull(secondTest)) {
				ret.add(receipt);
			}
		}
		REQUEST().setAttribute("offering", offering);
		REQUEST().setAttribute("receipts", ret);
		return SUCCESS;
	}

	public String add() {
		OfferingMember member = OfferingMemberService.INSTANCE
				.findById(this.memberId);
		SecondTest secondTest = new SecondTest();
		Date spokenDate = DateUtil.fromString(this.finalSpokenDate, this
				.i18n(this.i18n("format.date")));
		Date writtenDate = DateUtil.fromString(this.finalWrittenDate, this
				.i18n(this.i18n("format.date")));
		secondTest.setSecondSpokenDate(spokenDate);
		secondTest.setSecondWrittenDate(writtenDate);
		secondTest.setListening(this.listeningScore);
		secondTest.setSpeaking(this.speakingScore);
		secondTest.setGrammar(this.grammarScore);
		secondTest.setReading(this.readingScore);
		secondTest.setWriting(this.writtenScore);
		secondTest.setComment(this.comment);
		secondTest.setOfferingMember(member);

		secondTest = SecondTestService.INSTANCE.persist(secondTest);

		if (Checker.isNotNull(secondTest)) {
			String studentName = member.getReceipt().getStudent().getFullName();
			Offering offering = OfferingService.INSTANCE
					.findById(this.offeringId);
			this.addLoggingDetail(BaseAction.ADD, "logging.second.add",
					studentName, offering.getName());
		}
		return SUCCESS;
	}

	public String info() {
		Offering offering = OfferingService.INSTANCE.findById(this.offeringId);
		SecondTest secondTest = SecondTestService.INSTANCE.findById(this.id);
		REQUEST().setAttribute("offering", offering);
		REQUEST().setAttribute("secondTest", secondTest);
		REQUEST().setAttribute("receipt",
				secondTest.getOfferingMember().getReceipt());
		REQUEST().setAttribute("cashiers",
				UserService.INSTANCE.findByRole("Consulting"));
		return SUCCESS;
	}

	public String edit() {
		SecondTest secondTest = SecondTestService.INSTANCE.findById(this.id);
		Date spokenDate = DateUtil.fromString(this.finalSpokenDate, this
				.i18n(this.i18n("format.date")));
		Date writtenDate = DateUtil.fromString(this.finalWrittenDate, this
				.i18n(this.i18n("format.date")));
		secondTest.setSecondSpokenDate(spokenDate);
		secondTest.setSecondWrittenDate(writtenDate);
		secondTest.setListening(this.listeningScore);
		secondTest.setSpeaking(this.speakingScore);
		secondTest.setGrammar(this.grammarScore);
		secondTest.setReading(this.readingScore);
		secondTest.setWriting(this.writtenScore);
		secondTest.setComment(this.comment);

		secondTest = SecondTestService.INSTANCE.persist(secondTest);

		if (Checker.isNotNull(secondTest)) {
			String student = secondTest.getOfferingMember().getReceipt()
					.getStudent().getFullName();
			Offering offering = OfferingService.INSTANCE
					.findById(this.offeringId);
			this.addLoggingDetail(BaseAction.EDIT, "logging.second.edit",
					student, offering.getName());
		}

		return SUCCESS;
	}

	public String delete() {
		SecondTest secondTest = SecondTestService.INSTANCE.findById(this.id);
		Offering offering = OfferingService.INSTANCE.findById(this.offeringId);
		String student = secondTest.getOfferingMember().getReceipt()
				.getStudent().getFullName();
		if (SecondTestService.INSTANCE.deleteById(this.id)) {
			this.addLoggingDetail(BaseAction.DELETE, "logging.second.delete",
					student, offering.getName());
		}
		return SUCCESS;
	}

	public String report() {
		Offering offering = OfferingService.INSTANCE.findById(this.offeringId);
		List<SecondTest> secondTests = offering.getSecondFinalTests();

		boolean noted = false;
		Iterator<SecondTest> iterator = secondTests.iterator();
		while (iterator.hasNext() && !noted) {
			String comment = iterator.next().getComment();
			if (comment != null) {
				if (comment.length() > 10) {
					noted = true;
				}
			}
		}

		// Sure
		if (!secondTests.isEmpty()) {
			REQUEST().setAttribute("represent", secondTests.get(0));
		}

		REQUEST().setAttribute("noted", noted);
		REQUEST().setAttribute("offering", offering);
		REQUEST().setAttribute("secondTests", secondTests);

		this.addLoggingDetail(BaseAction.REPORT, "logging.second.report",
				offering.getName());

		return SUCCESS;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getMemberId() {
		return memberId;
	}

	public void setMemberId(Long memberId) {
		this.memberId = memberId;
	}

	public Long getReceiptId() {
		return receiptId;
	}

	public void setReceiptId(Long receiptId) {
		this.receiptId = receiptId;
	}

	public Long getOfferingId() {
		return offeringId;
	}

	public void setOfferingId(Long offeringId) {
		this.offeringId = offeringId;
	}

	public Double getWrittenScore() {
		return writtenScore;
	}

	public void setWrittenScore(Double writtenScore) {
		this.writtenScore = writtenScore;
	}

	public Double getSpeakingScore() {
		return speakingScore;
	}

	public void setSpeakingScore(Double speakingScore) {
		this.speakingScore = speakingScore;
	}

	public Double getListeningScore() {
		return listeningScore;
	}

	public void setListeningScore(Double listeningScore) {
		this.listeningScore = listeningScore;
	}

	public Double getGrammarScore() {
		return grammarScore;
	}

	public void setGrammarScore(Double grammarScore) {
		this.grammarScore = grammarScore;
	}

	public Double getReadingScore() {
		return readingScore;
	}

	public void setReadingScore(Double readingScore) {
		this.readingScore = readingScore;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public String getFinalSpokenDate() {
		return finalSpokenDate;
	}

	public void setFinalSpokenDate(String finalSpokenDate) {
		this.finalSpokenDate = finalSpokenDate;
	}

	public String getFinalWrittenDate() {
		return finalWrittenDate;
	}

	public void setFinalWrittenDate(String finalWrittenDate) {
		this.finalWrittenDate = finalWrittenDate;
	}
}
